package com.DeliveryProgram.www.dao;

import com.DeliveryProgram.www.po.*;
import com.DeliveryProgram.www.util.JDBCutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**用户类数据的存储*/
public class UserSave {
    private static Connection conn=null;
    private static Statement stmt=null;
    private static ResultSet rs=null;
    private static String sql=null;

    /**更改数据库中用户余额*/
    public static void changeMoney(User us){
        try {
            conn= JDBCutil.getConnection();
            sql="UPDATE USER SET money ="+us.getMoney()+" WHERE uid="+us.getUid();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**向数据库中输入新的地址*/
    public static void addAddress(User us){
        try {
            conn= JDBCutil.getConnection();
            sql="INSERT INTO address(USER,ad,ph) VALUES("+us.getUid()+",'"+us.getAddresses().get(us.getAddresses().size()-1).getAddr()+"','"+us.getAddresses().get(us.getAddresses().size()-1).getPhone()+"')";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**删除数据库中的地址*/
    public static void deleteAddr(Address addr){
        try {
            conn= JDBCutil.getConnection();
            sql="DELETE FROM address WHERE id="+addr.getId();
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**更新地址信息*/
    public static void updateAddr(User us,int num){
        try {
            conn= JDBCutil.getConnection();
            sql="UPDATE address SET ad ='"+us.getAddresses().get(num).getAddr()+"',ph='"+us.getAddresses().get(num).getPhone()+"' WHERE id="+us.getAddresses().get(num).getId();
            stmt=conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**将新的订单存入数据库*/
    public static void buy(User us, Order od, Business bu){
        try {
            conn=JDBCutil.getConnection();
            sql="INSERT INTO orders(USER,business,address,money,isComplete) VALUES("+us.getUid()+","+bu.getBid()+","+od.getAddress().getId()+","+od.getMoney()+",1)";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

    /**获取订单Id*/
    public static void getOrderId(User us,Order od,Business bu){
        try {
            conn=JDBCutil.getConnection();
            sql="SELECT id,DATE FROM orders WHERE USER="+us.getUid()+" AND business="+bu.getBid()+" AND address="+od.getAddress().getId()+" AND money="+od.getMoney();
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            rs.next();
            od.setId(rs.getInt(1));
            od.setDate(rs.getDate(2));
            od.setIsComplete(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(rs,stmt,conn);
        }
    }

    /**存取新订单的菜单*/
    public static void pushMenu(Menu mu,int id){
        try {
            conn=JDBCutil.getConnection();
            sql="INSERT INTO odmu(menu,mount,orde) VALUES('"+mu.getName()+"',"+mu.getMount()+","+id+")";
            stmt=conn.createStatement();
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }
    }

}
